package com.spider.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.*;

public class KuaiProxy {
    public static Map<String, String> getProxy() {
        String html = HttpClient.get("https://www.kuaidaili.com/free/inha/1/", "utf-8");
        List<Map<String, String>> proxyList = new ArrayList<Map<String, String>>();
        Document doc = Jsoup.parse(html);
        Elements trs = doc.select(".table tr");
        for (int i = 1; i < trs.size(); i++) {
            Elements tds = trs.get(i).select("td");
//            System.out.println(tds.get(0).text() + "\t" + tds.get(1).text());
            Map<String, String> map = new HashMap<String, String>();
            map.put(tds.get(0).text(), tds.get(1).text());
            proxyList.add(map);
        }
        Random random = new Random();
        return proxyList.get(random.nextInt(proxyList.size()));
    }
}
